<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <!-- 路由信息对象中的字段 -->
        {{ $route.params.id }}
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form ref="userInfoRef" :model="userInfo" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword">
                <el-input v-model="userInfo.newPassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!--子组件-->
            <user-info />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!--子组件 -->
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { reqGetUserProfile } from '@/api/user'
import { reqSaveUserDetailById } from '@/api/employee'
import UserInfo from './components/user-info.vue'
import JobInfo from './components/job-info.vue'
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo,
    JobInfo
  },
  data() {
    return {
      userInfo: {
        username: '',
        newPassword: '' // 让密码框不显示.....
      },
      rules: { // 校验规则
        username: [
          { required: true, message: '姓名不能为空', trigger: ['blur', 'change'] }
        ],
        newPassword: [
          { min: 6, max: 9, message: '密码必须是6-9位', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  computed: {
    userId() {
      return this.$route.params.id
    }
  },
  async created() {
    const { data } = await reqGetUserProfile(this.userId)
    this.userInfo = data
  },
  methods: {
    saveUser() {
      this.$refs.userInfoRef.validate(async boo => {
        if (!boo) return
        await reqSaveUserDetailById({ ...this.userInfo, password: this.userInfo.newPassword })
        this.$message.success('更新成功')
      })
    }
  }
}
</script>
